package com.lh.gomoku.clientUI;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;

public class ClientMain extends Shell {
	private Text serverIPtext;
	private Text serverPort;

	
	String serverIp;
	String serverPortString;
	
	public static BufferedReader scoketReader;
	public static BufferedWriter socketWriter;
	public static Socket socket;
	
	
	public static final String Operation_Success = "OK";
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			ClientMain shell = new ClientMain(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Create the shell.
	 * @param display
	 */
	public ClientMain(Display display) {
		super(display, SWT.SHELL_TRIM);
		
		Label serverIPLabel = new Label(this, SWT.NONE);
		serverIPLabel.setText("Server IP:");
		serverIPLabel.setBounds(65, 46, 49, 13);
		
		serverIPtext = new Text(this, SWT.BORDER);
		serverIPtext.setText("127.0.0.1");
		serverIPtext.setBounds(130, 40, 215, 19);
		
		Label serverPortLabel = new Label(this, SWT.NONE);
		serverPortLabel.setText("port:");
		serverPortLabel.setBounds(65, 82, 49, 13);
		
		serverPort = new Text(this, SWT.BORDER);
		serverPort.setText("9111");
		serverPort.setBounds(130, 76, 215, 19);
		
		Button connectButton = new Button(this, SWT.NONE);
		connectButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				serverIp = serverIPtext.getText();
				serverPortString = serverPort.getText();
				String result = connectToServer(serverIp,serverPortString);
				if(result.equals(Operation_Success)){
					Display display = Display.getDefault();
					Shell shell = new Shell(display);
					shell.setLayout(new FillLayout());
					new UserManagerUI(shell,SWT.NONE).setVisible(true);
					shell.pack(true);
					shell.open();
					shell.layout();
					close();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
					
				}else{
					
				}
			}
		});
		connectButton.setText("connect");
		connectButton.setBounds(176, 150, 68, 23);
		
		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("\u6B22\u8FCE\u4F7F\u7528\u6881\u5B8F\u4E94\u5B50\u68CB\u5BA2\u6237\u7AEF\u7A0B\u5E8F\uFF0C\u8F93\u5165\u670D\u52A1\u5668\u7684IP\u548C\u7AEF\u53E3\u540E\u8FDE\u63A5\u540E\u5373\u53EF\u5F00\u59CB\u6E38\u620F");
		label_2.setBounds(10, 209, 430, 23);
		createContents();
	}

	private String connectToServer(String serverIp,
			String serverPortString) {
		SocketAddress socketAdress = new InetSocketAddress(serverIp,Integer.parseInt(serverPortString));
		socket = new Socket();
		try {
			socket.connect(socketAdress);
			socket.setSendBufferSize(100);  
			scoketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return e.getMessage();
		}
		return Operation_Success;
	}
	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(450, 300);
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public static BufferedReader getScoketReader() {
		return scoketReader;
	}

	public static BufferedWriter getSocketWriter() {
		return socketWriter;
	}

	public static Socket getSocket() {
		return socket;
	}

}
